define('api', [
  'jquery'
], function ($) {

  var api = {};

  function ajaxPost(url, json, cb) {
    $.ajax({
      url: url,
      type: "POST",
      headers: {"Content-Type": "application/json;charset=UTF-8"},
      data: JSON.stringify(json),
      success: function (res) {
        cb && cb(null, res);
      },
      error: function (xhr) {
        cb && cb(xhr.responseText);
      }
    });
  }

  function ajaxGet(url, cb) {
    $.ajax({
      url: url,
      type: "GET",
      success: function (res) {
        cb && cb(null, res);
      },
      error: function (xhr) {
        cb && cb(xhr.responseText);
      }
    });
  }

  api.register = function register(name, pwd, userType, cb) {
    var url = '/api/v1/auth/register';
    var json = {name: name, pwd: pwd, userType: userType};
    ajaxPost(url, json, cb);
  };

  api.login = function login(name, pwd, userType, cb) {
    var url = '/api/v1/auth/login';
    var json = {name: name, pwd: pwd, userType: userType};
    ajaxPost(url, json, cb);
  };

  api.addLoc = function addLoc(cityName, zoneName, cb) {
    var url = '/api/v1/location';
    var json = {cityName: cityName, zoneName: zoneName};
    ajaxPost(url, json, cb);
  };

  api.allLoc = function allLoc(cb) {
    var url = '/api/v1/location';
    ajaxGet(url, cb);
  };

  api.addCate = function addCate(name, cb) {
    var url = '/api/v1/category';
    var json = {name: name};
    ajaxPost(url, json, cb);
  };

  api.allCate = function allCate(cb) {
    var url = '/api/v1/category';
    ajaxGet(url, cb);
  };

  api.addWare = function addWare(name, categoryId, quantity, price, cb) {
    var url = '/api/v1/vendor/ware';
    var json = {name: name, categoryId: categoryId, quantity: quantity, price: price};
    ajaxPost(url, json, cb);
  };

  api.allWare = function allWare(cb) {
    var url = '/api/v1/vendor/ware';
    ajaxGet(url, cb);
  };

  api.addShop = function addShop(name, locationId, cb) {
    var url = '/api/v1/vendor/shop';
    var json = {name: name, locationId: locationId};
    ajaxPost(url, json, cb);
  };

  api.allShop = function allShop(cb) {
    var url = '/api/v1/vendor/shop';
    ajaxGet(url, cb);
  };

  return api;
});
